package com.phospherus.common.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.phospherus.common.service.FormInstExportService;
import com.phospherus.common.util.JsonUtil;
import com.phospherus.flow.entity.DepositPay;
import com.phospherus.flow.entity.DocumentprocessCc;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangfx
 * @create: 2018-07-23 15:21
 **/
@Service
public class FormInstExportServiceImpl implements FormInstExportService{

    @Override
    public void export(Map<String, Object> allData, String moduleName, File exportFile) {
        Object data = null;
        String template = null;
        int sheetNum = 0;
        String listField = null;
        //5-单证员流程
        //tab2-单证员报关
        if ("documentprocess_cc_customsDeclarationInvoice".equals(moduleName)) { //报关发票
            DocumentprocessCc cc = (DocumentprocessCc) allData.get("documentprocess_cc");
            data = cc.getDocumentprocess_cc_customsDeclarationInvoice();
            template = "template/报关文件模版.xlsx";
            sheetNum = 0;
            listField = "documentprocess_cc_cdi_table";
        } else if ("documentprocess_cc_customsDeclarationBox".equals(moduleName)) { //报关箱单
            DocumentprocessCc cc = (DocumentprocessCc) allData.get("documentprocess_cc");
            data = cc.getDocumentprocess_cc_customsDeclarationBox();
            template = "template/报关文件模版.xlsx";
            sheetNum = 1;
            listField = "documentprocess_cc_cdb_table";
        } else if ("documentprocess_cc_customsDeclarationContract".equals(moduleName)) { //报关合同
            DocumentprocessCc cc = (DocumentprocessCc) allData.get("documentprocess_cc");
            data = cc.getDocumentprocess_cc_customsDeclarationContract();
            template = "template/报关文件模版.xlsx";
            sheetNum = 2;
        } else if ("documentprocess_cc_declarationForm".equals(moduleName)) { //报关单
            DocumentprocessCc cc = (DocumentprocessCc) allData.get("documentprocess_cc");
            data = cc.getDocumentprocess_cc_declarationForm();
            template = "template/报关文件模版.xlsx";
            sheetNum = 3;
            listField = "documentprocess_cc_df_table";
        } else if ("businessform_foreigntradecontract".equals(moduleName)) {
            //外贸合同
            data = allData.get("businessform_foreigntradecontract");
            template = "template/合同文件模板.xlsx";
            sheetNum = 0;
            listField = "bf_table1";
        } else if ("businessform_pi".equals(moduleName)) {
            //PI
            data = allData.get("businessform_pi");
            template = "template/合同文件模板.xlsx";
            sheetNum = 1;
            listField = "bp_table1";
        } else if ("businessform_domestictradecontract".equals(moduleName)) {
            //内贸合同
            data = allData.get("businessform_domestictradecontract");
            data=((List) data).get(0);
            template = "template/合同文件模板.xlsx";
            sheetNum = 2;
            listField = "bd_table1";
        } else if ("businessform_profitcalculation".equals(moduleName)) {
            //业务利润核算表
            data = allData.get("businessform_profitcalculation");
            template = "template/合同文件模板.xlsx";
            sheetNum = 3;
            listField = "bpc_table1";
        } else if ("businessform_paymentnotice".equals(moduleName)) {
            //付款通知单
            data = allData.get("businessform_paymentnotice");
            template = "template/合同文件模板.xlsx";
            sheetNum = 4;
            listField = "bpn_table1";
        } else if ("businessform_depositpay".equals(moduleName)) {
            //保证金付款发票
            data = allData.get("businessform_depositpay");
            template = "template/合同文件模板.xlsx";
            sheetNum = 5;
            listField = "bdp_table1";
        } else {
            throw new RuntimeException("moduleName错误！");
        }


        Map<String, Object> dataMap = JsonUtil.fromJson(JsonUtil.toJson(data));
        if(listField!=null){
            List<Map<String, Object>> list = (List<Map<String, Object>>) dataMap.get(listField);
            for(int i=0; i<list.size(); i++){
                list.get(i).put("xuhao", String.valueOf(i+1));
                if("businessform_profitcalculation".equals(moduleName)){
                    list.get(i).put("bpc_table1_taxrate",list.get(i).get("bpc_table1_taxrate")+"%");
                }
            }
            if(list.size() == 1){
                list.add(new HashMap<>());
            }
        }
        export(template, sheetNum, dataMap, exportFile);
    }
    private void export(String template, int sheetNum, Map<String, Object> dataMap, File exportFile){
        if(!exportFile.getParentFile().exists()){
            exportFile.getParentFile().mkdirs();
        }
        TemplateExportParams params = new TemplateExportParams(
                template, sheetNum);
        Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap);
        try (FileOutputStream fos = new FileOutputStream(exportFile);){
            workbook.write(fos);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
